package com.yuntsg.ruiijn.databse_search.service;

import cn.hutool.http.HtmlUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.yuntsg.nnsfcp.common.utils.RedisUtil;
import com.yuntsg.ruiijn.database.mapper.FileDetailsTxtMapper;
import com.yuntsg.ruiijn.database.mapper.WosBasicMapper;
import com.yuntsg.ruiijn.databse_search.BaseService;
import com.yuntsg.ruiijn.databse_search.entity.FileDetailsTxt;
import com.yuntsg.ruiijn.paperana.utils.esutils.EsServerEntity;
import common.util.*;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
@DS("master")
@SuppressWarnings("all")

public class FileDetailTxtService extends BaseService<FileDetailsTxtMapper, FileDetailsTxt> {
    @Autowired
    private ElasticsearchClient restClient;
    @Autowired
    private ExtractUtil extractUtil;
    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private SearchSelfService searchSelfService;
    @Autowired
    private BussService bussService;
    @Autowired
    private ImageComparison imageComparison;

    @Autowired
    private WosBasicMapper wosBasicMapper;
    @Autowired

    private RedisUtil redisUtil;
    @Autowired

    private CutImageUtil cutImageUtil;
    @Autowired
    private FeatureService featureService;


    @Value("${upload.path}")
    public String uploadPath;


    @Value("${upload.tempPath}")
    public String tempPath;


    // 2023年10月5日15:06:59 进行匹配es相似片段
    @SneakyThrows
    public boolean matchEsRes(String str, Double simi) {
        if (simi == null) {
            simi = 0.3;
        }
        boolean isMath = false;
        List<EsServerEntity> hits = searchSelfService.pubParticiplesRetrieval(str, 0, 1);
        if (hits.size() > 0) {
            EsServerEntity hit = hits.get(0);
            double str1 = StfUtil.genManualScore(str, HtmlUtil.cleanHtmlTag(hit.getHighlight().get("str").get(0)));
            if (str1 >= simi) {
                isMath = true;
            }
        }
        return isMath;

    }

    // 2023年10月5日15:06:59 进行匹配es相似片段
    @SneakyThrows
    public boolean matchEsResManal(String str, Integer fid, Integer uid, Double simi) {
        boolean isMath = false;
        List<Hit<JSONObject>> hits = searchSelfService.manParticiplesRetrieval(str, 0, 1, fid, uid);
        if (hits.size() > 0) {
            Hit<JSONObject> hit = hits.get(0);
            double str1 = StfUtil.genManualScore(str, HtmlUtil.cleanHtmlTag(hit.highlight().get("str").get(0)));
            if (str1 >= simi) {
                isMath = true;
            }
        }
        return isMath;

    }


}
